var express = require('express')
// const { read } = require('fs')
var app = express()
app.use(express.static('static'))

// post请求参数的中间件
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
//定义数据
var arr = [
    { email: '102524056@qq.com', name: '张三', phone: '13611129070', pass: '1234', ingroup: '开发组', isadmin: false, status: true },
    { email: '256354896@qq.com', name: '浅影', phone: '18945623886', pass: '1234', ingroup: '销售组', isadmin: true, status: true },
    { email: '156486523@qq.com', name: '李沁', phone: '15529362823', pass: '1234', ingroup: '运营组', isadmin: true, status: false },
    { email: '102524056@qq.com', name: '张三', phone: '13611129070', pass: '1234', ingroup: '开发组', isadmin: false, status: true },
    { email: '256354896@qq.com', name: '浅影', phone: '18945623886', pass: '1234', ingroup: '销售组', isadmin: true, status: true },
    { email: '156486523@qq.com', name: '李沁', phone: '15529362823', pass: '1234', ingroup: '运营组', isadmin: true, status: false },
    { email: '102524056@qq.com', name: '张三', phone: '13611129070', pass: '1234', ingroup: '开发组', isadmin: false, status: true },
    { email: '256354896@qq.com', name: '浅影', phone: '18945623886', pass: '1234', ingroup: '销售组', isadmin: true, status: true },
    { email: '156486523@qq.com', name: '李沁', phone: '15529362823', pass: '1234', ingroup: '运营组', isadmin: true, status: false },
    { email: '102524056@qq.com', name: '张三', phone: '13611129070', pass: '1234', ingroup: '开发组', isadmin: false, status: true },
    { email: '256354896@qq.com', name: '浅影', phone: '18945623886', pass: '1234', ingroup: '销售组', isadmin: true, status: true },
    { email: '156486523@qq.com', name: '李沁', phone: '15529362823', pass: '1234', ingroup: '运营组', isadmin: true, status: false },
    { email: '102524056@qq.com', name: '张三', phone: '13611129070', pass: '1234', ingroup: '开发组', isadmin: false, status: true },
    { email: '256354896@qq.com', name: '浅影', phone: '18945623886', pass: '1234', ingroup: '销售组', isadmin: true, status: true },
    { email: '156486523@qq.com', name: '李沁', phone: '15529362823', pass: '1234', ingroup: '运营组', isadmin: true, status: false },
    { email: '102524056@qq.com', name: '张三', phone: '13611129070', pass: '1234', ingroup: '开发组', isadmin: false, status: true },
    { email: '256354896@qq.com', name: '浅影', phone: '18945623886', pass: '1234', ingroup: '销售组', isadmin: true, status: true },
    { email: '156486523@qq.com', name: '李沁', phone: '15529362823', pass: '1234', ingroup: '运营组', isadmin: true, status: false },
]
// 设置接口
// 获取列表
app.get('/user/list', (req, res) => {
    var { page, count } = req.query;

    var newarr = arr.slice((page - 1) * count, page * count)
    // 返回的数据
    res.send({ code: 200, msg: '请求成功', list: newarr })
    // console.log(page);
    // 得到的参数
    // console.log(req.query);
})
// 登录
app.post('/user/login', (req, res) => {
    var { phone, pass } = req.body
    var i = arr.findIndex((item) => {
        return item.phone == phone && item.pass == pass
    })
    // console.log(phone);
    // console.log(req.body);
    // console.log(i);
    if (i == -1) {
        res.send({ code: 400, msg: '收到数据，登录失败' })
    } else {
        res.send({ code: 200, msg: '收到数据，登录成功' })
    }
})
// 模糊查询
app.get('/user/search', (req, res) => {
    var { searchKey } = req.query
    var arr2 = arr.filter((item) => {
        return item.name.includes(searchKey)
    })
    console.log(arr2);
    res.send({ code: 200, msg: "查询成功", list: arr2 })
})
app.listen(8080, () => {
    console.log('服务端运行中');
})